package org.yookiee.support.core.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.common.base.Strings;
import org.yookiee.support.core.EasyLocale;
import org.yookiee.support.core.EasyTimeZone;
import org.yookiee.support.core.I18NContext;

import java.io.IOException;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * FileName CustomLocalDateTimeDeserializer
 * Package org.yookiee.support.core.deserializers
 * Description
 * author cpstory
 * create 2023-11-15 13:50
 * version V1.0
 */
public class CustomLocalTimeDeserializer extends JsonDeserializer<LocalTime> {

    private DateTimeFormatter formatter;

    public CustomLocalTimeDeserializer() {
    }

    public CustomLocalTimeDeserializer(DateTimeFormatter formatter) {
        super();
        this.formatter = formatter;
    }

    @Override
    public LocalTime deserialize(JsonParser parser, DeserializationContext context)
            throws IOException {
        System.out.println("开始进行反序列化");
        EasyLocale locale = I18NContext.get();

        if(!Strings.isNullOrEmpty(locale.getTimeZone())){
            EasyTimeZone timeZone = EasyTimeZone.formatByTimeZone(locale.getTimeZone());
            assert timeZone != null;
            return convertLocalTime(LocalTime.parse(parser.getText(), formatter), ZoneId.of(timeZone.getTimezone()),
                    ZoneId.systemDefault());
        }else{
            return convertLocalTime(LocalTime.parse(parser.getText(), formatter), ZoneId.systemDefault(),
                    ZoneId.systemDefault());
        }

    }

    /**
     * 时区转换
     *
     * @param localTime
     * @param originZoneId
     * @param targetZoneId
     * @return
     */
    public static LocalTime convertLocalTime(LocalTime localTime, ZoneId originZoneId,
                                                     ZoneId targetZoneId) {
        return localTime;
    }
}
